package com.bsg.upm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.bsg.upm.cache.DefinitionMetricCache;
import com.bsg.upm.entity.DefinitionMetricEntity;
import com.bsg.upm.exception.APIException;

/**
 * 
 * @author yucs
 *
 */
@Service
public class MonitorSubServService extends BaseService {
	
	public static final String MONITOR_STATUS = "status";

	
	@Resource
	private DefinitionMetricCache definitionMetricCache;

	@Resource
	private SubServService subServService;

//	public JSONArray listMonitorSubServStatus(SubServEntity serv) throws APIException {
//
//		if (serv == null) {
//			return null;
//		}
//
//		String type = serv.getDefinitionSubServ().getName();
//		String siteId = serv.getServ().getArea().getId();
//		String relatesubServId = serv.getRelateId();
//
//		String metric = definitionMetricCache.getMetric(type, MONITOR_STATUS);
//
//		Map<String, JSONArray> maps = getNohistory(siteId, metric, relatesubServId);
//		if (maps.isEmpty()) {
//			return new JSONArray();
//		}
//
//		return maps.get(relatesubServId);
//	}

	public Map<String, JSONArray> getAllSubServStatusByType(String siteId, String type) throws APIException {

		String metric = definitionMetricCache.getMetric(type,MONITOR_STATUS);

		Map<String, JSONArray> maps = getNohistory(siteId, metric, null);

		return maps;

	}

	public Map<String, JSONArray> getAllUnitMonitorStatusJsonArrMap(String siteId) throws APIException {
		Map<String, JSONArray> maps = new HashMap<String, JSONArray>();

		List<DefinitionMetricEntity> definitions = definitionMetricCache.listDfMetricFromCache();

		for (DefinitionMetricEntity df : definitions) {

			if (df.getName().equals(MONITOR_STATUS)) {
				Map<String, JSONArray> tmpMap = getNohistory(siteId, df.getMetric(), null);
				maps.putAll(tmpMap);
			}

		}

		return maps;

	}
	
}
